/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.fw.types;

import javax.xml.bind.annotation.XmlRootElement;

import net.interknit.knitter.fw.util.ReflectionHelper;

@XmlRootElement(name = "net.interknit.knitter.fw.types.AnyMap")
public class AnyMap
{
	/**
	 * The underlying state maintained by this adapter
	 */
	private java.util.Map<Any, Any> state = defaultState();

	/**
	 * Private method to provide the "default" state for this adapter
	 */
	private java.util.Map<Any, Any> defaultState()
	{
		return state != null ? state : (state = new java.util.HashMap<Any, Any>());
	}

	/**
	 * Private method to provide the "default" value for this adapter
	 */
	private java.util.Map<java.lang.Object, java.lang.Object> defaultValue(java.util.Map<java.lang.Object, java.lang.Object> v)
	{
		return v != null ? v : new java.util.HashMap<java.lang.Object, java.lang.Object>();
	}

	/**
	 * Default no-arg constructor
	 */
	public AnyMap() { ; }

	/**
	 * Construct this instance from the Object parameter.
	 *
	 * @throws ClassCastException if the parameter obj is not of type java.util.Map<java.lang.String, java.util.List<java.lang.String>>
	 */
	public AnyMap(java.lang.Object obj)
	{
		this((java.util.Map<java.lang.Object, java.lang.Object>) obj);
	}

	/**
	 * Constructor to accept the user values and convert it to the underlying state.
	 * In other words, this constructor provides a way for "wrapping" the user value into this adapter.
	 * This constructor is also known as 'value constructor'
	 */
	public AnyMap(java.util.Map<java.lang.Object, java.lang.Object> value)
	{
		for(java.util.Iterator<java.lang.Object> it = defaultValue(value).keySet().iterator(); it.hasNext(); )
		{
			java.lang.Object k = it.next();
			java.lang.Object v = defaultValue(value).get(k);
			defaultState().put(new Any(k), new Any(v));
		}
	}

	/**
	 * Adapter method to convert the underlying state back to user value.
	 * In other words, this method provides a way for "unwrapping" the user value from this adapter that
	 * was previously wrapped using the value constructor.
	 */
	public <T extends java.util.Map<java.lang.Object, java.lang.Object>> T unwrap(Class<T> type)
	{
		T map = ReflectionHelper.createInstance(type);
		for(java.util.Iterator<Any> it = defaultState().keySet().iterator(); it.hasNext(); )
		{
			Any k = it.next();
			Any v = defaultState().get(k);
			map.put(k.unwrap(), v.unwrap());
		}
		return map;
	}

	/**
	 * Return the underlying state of this adapter.
	 * Note: the underlying state must not be modified or manipulated as part of this method,
	 * this method should act as a simple getter method for the underlying state.
	 *
	 * @return the underlying state of this adapter
	 */
	public java.util.Map<Any, Any> getMap()
	{
		return defaultState();
	}

	/**
	 * Sets the underlying state of this adapter with the given state parameter.
	 * Note: the underlying state must not be modified or manipulated as part of this method,
	 * this method should act as a simple setter method for the underlying state.
	 *
	 * @param state the underlying state to set
	 */
	public void setMap(java.util.Map<Any, Any> state)
	{
		this.state = state;
	}
}

